package model

import (
	"github.com/gomodule/redigo/redis"
	"strconv"
	"tizhi_front/nspel"
)

type CourseLike struct {
	Id        int64 `gorm:"AUTO_INCREMENT;primary_key;column:id"`
	StudentId int64 `gorm:"column:student_id"`
	CourseId  int64 `gorm:"column:course_id"`
}

func (*CourseLike) TableName() string {
	return "course_like"
}

type CourseLikeE struct{}

func (c *CourseLikeE) SelectByStudentIdCourseId(studentId int64, courseId int64) (err error) {
	var courseLike CourseLike0
	key := "course_like0.student_id:" + strconv.FormatInt(studentId, 10) + ".course_id:" + strconv.FormatInt(courseId, 10)
	err = nspel.RedisGetGob(key, &courseLike)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("course_id = ?", courseId).First(&courseLike).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, courseLike)
	return
}

type CourseLike0 struct {
	Id int64 `gorm:"primary_key;column:id"`
}

func (*CourseLike0) TableName() string {
	return "course_like"
}

func (c *CourseLike0) Delete(studentId int64, courseId int64) (err error) {
	err = nspel.GetOrm().Delete(c).Error
	if err != nil {
		return
	}
	_ = nspel.RedisKeysDelete("course_like0.student_id:" + strconv.FormatInt(studentId, 10) + ".course_id:" + strconv.FormatInt(courseId, 10))
	return
}

func (c *CourseLike0) SelectByStudentIdCourseId(studentId int64, courseId int64) (err error) {
	key := "course_like0.student_id:" + strconv.FormatInt(studentId, 10) + ".course_id:" + strconv.FormatInt(courseId, 10)
	err = nspel.RedisGetGob(key, c)
	if err == nil {
		return
	}
	if err != redis.ErrNil {
		nspel.Error(err)
	} else {
		err = nil
	}
	if err = nspel.GetOrm().Select("id").Where("student_id = ?", studentId).Where("course_id = ?", courseId).First(c).Error; err != nil {
		return
	}
	nspel.RedisSetGob(key, c)
	return
}

type CourseLike1 struct {
	StudentId int64 `gorm:"column:student_id"`
	CourseId  int64 `gorm:"column:course_id"`
}

func (*CourseLike1) TableName() string {
	return "course_like"
}

func (c *CourseLike1) Insert() (err error) {
	err = nspel.GetOrm().Create(c).Error
	return
}
